<!--
  #%L
  thinkbig-ui-feed-manager
  %%
  Copyright (C) 2017 ThinkBig Analytics
  %%
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  #L%
  -->
<vertical-section-layout section-title="调度" on-save-edit="vm.onSave()" on-edit="vm.onEdit()" on-cancel-edit="vm.onCancelEdit()"
                         allow-edit="!vm.model.reusableFeed && vm.allowEdit" editable="vm.editableSection" keep-editable-after-save="true" is-valid="vm.isValid"
                         the-form="vm.scheduleFeedForm">

    <readonly-section>
        <div ng-if="vm.model.reusableFeed == false">
        <div layout="row" class="layout-padding-top-bottom">
            <span flex="25" class="property-name">策略</span>

            <div flex>
                {{vm.model.schedule.schedulingStrategy}}
            </div>
        </div>
            <div layout="row" class="layout-padding-top-bottom" ng-if="vm.model.schedule.schedulingStrategy != 'TRIGGER_DRIVEN'">
            <span flex="25" class="property-name">周期</span>

            <div flex>
                {{vm.model.schedule.schedulingPeriod}}
            </div>
        </div>
            <div layout="row" class="layout-padding-top-bottom" ng-if="vm.isClustered && vm.supportsExecutionNode">
                <span flex="25" class="property-name">执行节点</span>

                <div flex>
                    {{vm.model.schedule.executionNode}}
                </div>
            </div>
            <div ng-if="vm.model.schedule.schedulingStrategy == 'TRIGGER_DRIVEN'">
                <div layout="column">
                    <div class="md-subhead">运行前提条件</div>
                </div>
                <div ng-if="vm.model.schedule.preconditions != null && vm.model.schedule.preconditions.length >0 " layout-fill>
                    <md-list class="md-2-line">
                        <md-list-item ng-repeat="rule in vm.model.schedule.preconditions track by $index" layout-fill class="with-padding layout-padding-top-bottom">
                            <div layout="column" flex="75" class="md-list-item-text">
                                <span>{{rule.propertyValuesDisplayString}}</span>
                                <span class="hint">{{rule.name}}</span>
                            </div>
                            <md-divider ng-if="!$last"></md-divider>
                        </md-list-item>
                    </md-list>
                </div>
            </div>
        </div>
        <div ng-if="vm.model.reusableFeed == true">
            这是一个抽象的可重复使用的管道。 此管道未应用任何计划。 引用此管道的其他模板将提供其计划        </div>
    </readonly-section>
    <editable-section>

        <ng-form name="vm.scheduleFeedForm">
            <div layout-gt-sm="row" style="margin-bottom:18px">

                <md-input-container class="md-block" flex-gt-sm>
                    <label>策略</label>
                    <md-select ng-model="vm.editModel.schedule.schedulingStrategy" ng-change="vm.onScheduleStrategyChange()">
                        <md-option ng-value="opt.value" ng-repeat="opt in vm.scheduleStrategies">{{ opt.label }}</md-option>
                    </md-select>
                </md-input-container>


                <md-input-container class="md-block" flex-gt-sm ng-if="vm.editModel.schedule.schedulingStrategy == 'CRON_DRIVEN'">
                    <label>表达式</label>
                    <input required ng-model="vm.editModel.schedule.schedulingPeriod" name="cronExpression"
                           cron-expression-validator
                           ng-model-options="{ updateOn: 'default blur', debounce: {'default': 300, 'blur': 0} }"/>

                    <div ng-show="vm.scheduleFeedForm.cronExpression.$touched && vm.scheduleFeedForm.cronExpression.$invalid"
                         ng-messages="vm.scheduleFeedForm.cronExpression.$error">
                        <div ng-show="vm.scheduleFeedForm.cronExpression.$error.cronExpression"
                             ng-message="cronExpression">无效的Cron表达式
                        </div>
                        <div ng-show="vm.scheduleFeedForm.cronExpression.$error.required" ng-message="required">
                            Required.
                        </div>
                    </div>
                    <cron-expression-preview cron-expression="vm.editModel.schedule.schedulingPeriod"
                                             ng-if="vm.editModel.schedule.schedulingStrategy == 'CRON_DRIVEN' && vm.scheduleFeedForm.cronExpression.$valid"></cron-expression-preview>
                </md-input-container>

                <div ng-if="vm.editModel.schedule.schedulingStrategy == 'TIMER_DRIVEN' || vm.editModel.schedule.schedulingStrategy == 'PRIMARY_NODE_ONLY'" layout="row" style="margin-bottom:-18px">
                    <md-input-container class="md-block" flex-gt-sm>
                        <label>运行调度</label>
                        <input ng-model="vm.timerAmount" required string-to-number type="number" ng-change="vm.timerChanged()"
                               ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }"/>
                    </md-input-container>

                    <md-input-container class="md-block" flex-gt-sm>
                        <label>单位</label>
                        <md-select ng-model="vm.timerUnits" ng-change="vm.timerChanged()">
                            <md-option value="days">天</md-option>
                            <md-option value="hrs">小时</md-option>
                            <md-option value="min">分</md-option>
                            <md-option value="sec">秒</md-option>
                        </md-select>
                    </md-input-container>
                </div>
            <!--
            <md-input-container class="md-block" flex-gt-sm>
                <label>Concurrent Tasks</label>
                <input type="number" ng-model="vm.editModel.schedule.concurrentTasks">
                </input>
            </md-input-container>

            -->
            </div>
            <div ng-if="vm.isClustered && vm.supportsExecutionNode" style="margin:18px 0">
                <md-input-container class="md-block">
                    <label>执行节点</label>
                    <md-select ng-model="vm.editModel.schedule.executionNode" required>
                        <md-option value="ALL">所有节点</md-option>
                        <md-option value="PRIMARY">主节点</md-option>
                    </md-select>
                </md-input-container>
            </div>
            <div ng-if="vm.editModel.schedule.schedulingStrategy == 'TRIGGER_DRIVEN'">
                <div layout="column">
                    <div class="md-subhead">运行前提条件</div>
                    <div class="hint">指定执行依赖关系</div>
                </div>
                <div layout="column" layout-align="start start" class="layout-padding-top-bottom">

                    <div ng-if="vm.editModel.schedule.preconditions != null && vm.editModel.schedule.preconditions.length >0 " layout-fill>
                        <md-list>
                            <md-list-item ng-repeat="rule in vm.editModel.schedule.preconditions track by $index" layout-fill class="layout-padding-top-bottom">
                                <div layout="column" flex="75" class="md-list-item-text">
                                    <span>{{rule.propertyValuesDisplayString}}</span>
                                    <span class="hint">{{rule.name}}</span>
                                </div>
                           <span class="md-secondary">
                                  <md-button class="md-icon-button" ng-click="vm.showPreconditionDialog($index)">
                                      <ng-md-icon icon="edit"></ng-md-icon>
                                  </md-button>
                            </span>
                                <md-divider ng-if="!$last"></md-divider>
                            </md-list-item>
                        </md-list>
                    </div>
                    <md-button class="md-primary" ng-click="vm.showPreconditionDialog()">
                        添加前提条件
                    </md-button>
                </div>

            </div>

        </ng-form>
    </editable-section>
</vertical-section-layout>
